/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.Exercise.DeleteExercise;

import com.myapp.model.DAO.Notifications.NotificationsDao;
import com.myapp.model.ejercicios.Ejercicios;
import com.myapp.model.notificaciones.Notificaciones;
import com.myapp.model.resueltos.Resueltos;
import com.myapp.model.tipos.Tipos;
import com.myapp.model.usuarios.Usuarios;
import com.myapp.struts.Exercise.NewExercise.NewExerciseForm;
import com.myapp.svc.Exercise.ExerciseSvc;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.MessageResources;

/**
 *
 * @author Administrador
 */
public class DeleteExerciseAction extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String FAIL = "fail";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception0
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ActionForward resultado = new ActionForward();
        Integer id = Integer.parseInt(request.getParameter("id"));

        ExerciseSvc exerSvc = new ExerciseSvc();
        Ejercicios exer = new Ejercicios();
        exer.setIdejercicio(id);       
        
        boolean exito = exerSvc.deleteExercise(exer);
        
        if(exito){
            
            resultado = mapping.findForward(SUCCESS);
        
        }else{
            
            ActionErrors errors = new ActionErrors();

            //Recupero el fichero de recursos
            MessageResources m;
            m = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);

            //Idioma actual
            java.util.Locale idioma = (java.util.Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);
            
            errors.add("result", new ActionMessage("error.lockDel", m.getMessage(idioma, "error.lockDel")));
            
            saveErrors(request, errors);
            
            
            resultado = mapping.findForward(FAIL);       
        }

        return resultado;
    }
}
